package com.balance.anole.erp.common.dialog;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.balance.anole.business.vo.ProductItemSearchTO;
import com.balance.anole.common.to.CodeLookTO;
import com.balance.anole.erp.component.CodeLookComboViewer;
import com.balance.anole.erp.util.DataConstants;
import com.balance.anole.erp.util.Messages;

public class ProductSelectorSearchPanel extends Group {

	private Button searchButton;

	private ProductItemSearchTO ps = new ProductItemSearchTO();;

	private CodeLookComboViewer productType;

	private Text styleSn;

	private Text productName;

	private Text productSn;
	/**
	 * Create the composite
	 * @param parent
	 * @param style
	 */
	public ProductSelectorSearchPanel(Composite parent, int style, final boolean isClient) {
		super(parent, style);
		setText("查询条件");

		this.setLayout(new GridLayout(2, false));

		final Label productsnLabel = new Label(this, SWT.NONE);
		GridData labelGd = new GridData();
		labelGd.widthHint = 100;
		productsnLabel.setLayoutData(labelGd);
		productsnLabel.setText(Messages.ProductSelector_Prod_Sn_Label);

		productSn = new Text(this, SWT.BORDER);
		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, true, false);
		gd.widthHint = 200;
		productSn.setLayoutData(gd);

		final Label productnameLabel = new Label(this, SWT.NONE);
		productnameLabel.setLayoutData(labelGd);
		productnameLabel.setText(Messages.ProductSelector_Prod_Name_Label);

		productName = new Text(this, SWT.BORDER);
		productName.setLayoutData(gd);

		final Label producttypeLabel = new Label(this, SWT.NONE);
		producttypeLabel.setLayoutData(labelGd);
		producttypeLabel.setText(Messages.ProductSelector_Prod_Type_Label);

		if(isClient){
			productType = new CodeLookComboViewer(DataConstants.SELECTION_TYPE_PROD_TYPE, this, SWT.READ_ONLY,true);
		}else{
			productType = new CodeLookComboViewer(DataConstants.SELECTION_TYPE_PROD_TYPE, this, SWT.READ_ONLY);
		}
		productType.setLayoutData(gd);

		final Label typesnLabel = new Label(this, SWT.NONE);
		typesnLabel.setLayoutData(labelGd);
		typesnLabel.setText(Messages.ProductSelector_Prod_Type_Sn_Label);

		styleSn = new Text(this, SWT.BORDER);
		styleSn.setLayoutData(gd);

		searchButton = new Button(this, SWT.NONE);
		GridData gd1 = new GridData();
		gd1.horizontalSpan = 2;
		gd1.grabExcessHorizontalSpace = true;
		gd1.horizontalAlignment = GridData.HORIZONTAL_ALIGN_CENTER;
		searchButton.setLayoutData(gd1);
		searchButton.setText(Messages.ProductSelector_Search_Btn_Label);

		Control[] tabList = new Control[]{productSn, productName, productType.getCombo(), styleSn, searchButton};
		this.setTabList(tabList);

		dataBinding();
		KeyListener kl = new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
			}
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.keyCode == SWT.CR){
					Event event = new Event();
					event.type = SWT.Selection;
					event.widget = searchButton;
					searchButton.notifyListeners(SWT.Selection, event);
				}
			}

		};
		productSn.addKeyListener(kl);
		productName.addKeyListener(kl);
		productType.getCombo().addKeyListener(kl);
		styleSn.addKeyListener(kl);

	}

	public void initSearchCriteria(){
		productType.setSelection(new StructuredSelection(CodeLookComboViewer.EMPTY_SELECTION));
	}

	/**
	 * If all criteria are empty, then return false.
	 * @return
	 */
	public boolean isSearchCriteriaValid(){
		boolean result = true;
		if (StringUtils.isEmpty(productName.getText())
				&& StringUtils.isEmpty(productSn.getText())
				&& StringUtils.isEmpty(styleSn.getText())) {
			CodeLookTO selectedType = (CodeLookTO) ((StructuredSelection) productType
					.getSelection()).getFirstElement();
			if (CodeLookComboViewer.EMPTY_SELECTION.getCodeTypeId()
					.equalsIgnoreCase(selectedType.getCodeTypeId())
					&& CodeLookComboViewer.EMPTY_SELECTION.getCodeId()
							.equalsIgnoreCase(selectedType.getCodeId())) {
				result = false;
			}
		}
		return result;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	public Button getSearchButton() {
		return searchButton;
	}

	public ProductItemSearchTO getSearchTO(){
		return this.ps;
	}

	private void dataBinding(){
		DataBindingContext bindingContext = new DataBindingContext();

		bindingContext.bindValue(SWTObservables.observeText(productSn, SWT.Modify), BeansObservables.observeValue(ps, "productItemId"), //$NON-NLS-1$
				null, null);
		bindingContext.bindValue(SWTObservables.observeText(styleSn, SWT.Modify), BeansObservables.observeValue(ps, "styleSn"), //$NON-NLS-1$
				null, null);
		bindingContext.bindValue(SWTObservables.observeText(productName, SWT.Modify), BeansObservables.observeValue(ps, "productDesc"), //$NON-NLS-1$
				null, null);
		bindingContext.bindValue(ViewersObservables.observeSingleSelection(productType), BeansObservables.observeValue(ps, "productTypeId"), //$NON-NLS-1$
				null, null);
	}
}
